<template>
  <div class="orders">
    <HeaderCom title="订单管理" titsub="订单列表">
      <div>
        <el-row :gutter="20">
          <el-col :span="8">
            <el-input clearable @clear="clearSearch" placeholder="请输入内容" v-model="orderSearch">
              <el-button slot="append" icon="el-icon-search" @click="searchUsers"></el-button>
            </el-input>
          </el-col>
        </el-row>
      </div>
      <el-table :data="orderList" style="width: 100%" border>
        <el-table-column type="index" width="50"></el-table-column>
        <el-table-column prop="order_number" label="订单编号"></el-table-column>
        <el-table-column prop="order_price" label="订单价格" width="100"></el-table-column>
        <el-table-column prop="order_pay" label="是否付款" width="120">
          <template v-slot="scope">
            <!-- {{scope.row}} -->
            <el-button type="danger" v-show="scope.row.order_pay" plain>未付款</el-button>
            <el-button type="success" v-show="!scope.row.order_pay" plain>已付款</el-button>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货" width="100"></el-table-column>
        <el-table-column prop="create_time" label="下单时间" width="180">
          <template v-slot="scope">{{scope.row.create_time | dateformat}}</template>
        </el-table-column>
        <el-table-column prop="edit" label="操作" width="180">
          <template v-slot="scope">
            <!-- {{scope.row}} -->
            <el-button type="primary" size="mini" icon="el-icon-edit" @click="editOrder(scope.row)"></el-button>
            <el-button type="success" size="mini" icon="el-icon-setting" @click="flowOrder"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[10, 20]"
        :page-size="10"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </HeaderCom>
    <el-dialog title="修改地址" :visible.sync="dialogOrderVisible" width="50%" class="catedialog">
      <el-form :model="orderAddress" :rules="cateRules" ref="orderForm" label-width="100px">
        <el-form-item label="省市区/县">
          <el-cascader :options="cityData" v-model="orderAddress.address" :clearable="true"></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="city">
          <el-input v-model="orderAddress.des" :autofocus="true"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogOrderVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogOrderVisible = falsed">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog title="物流地址" :visible.sync="dialogFlowVisible" width="50%" class="catedialog">
      <el-timeline>
        <el-timeline-item
          v-for="(activity, index) in orderAddress.address"
          :key="index"
          :timestamp="activity.ftime"
        >{{activity.context}}</el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
import HeaderCom from '../Header/HeaderCom'
import moment from 'moment'
import cityData from '../../assets/js/citydata'
export default {
  data() {
    return {
      queryOrder: {
        query: '',
        pagenum: 1,
        pagesize: 10
        // user_id: ''
        // pay_status: '1',
        // is_send: '',
        // order_fapiao_title: '',
        // order_fapiao_company: '',
        // order_fapiao_content: '',
        // consignee_addr: ''
      },
      total: 0, // 总条数
      orderList: [],
      currentPage: 1,
      orderSearch: '',
      dialogOrderVisible: false, // 编辑商品弹窗
      cityData,
      orderAddress: {
        address: [],
        des: ''
      },
      cateRules: {
        address: [{ required: true, message: '请输入地址', trigger: 'blur' }]
      },
      dialogFlowVisible: false
    }
  },
  created() {
    this.getOrders()
  },
  components: {
    HeaderCom
  },
  methods: {
    async getOrders() {
      const { data: res } = await this.$axios.get('orders', {
        params: this.queryOrder
      })
      if (res.meta.status !== 200) return this.$message.error('获取数据失败')
      this.orderList = res.data.goods
      this.total = res.data.total
    },
    handleSizeChange(size) {
      this.queryOrder.pagesize = size
      this.getOrders()
    },
    handleCurrentChange(curPage) {
      this.queryOrder.pagenum = curPage
      this.getOrders()
    },
    searchUsers() {
      this.queryOrder.query = this.orderSearch
      this.getOrders()
    },
    clearSearch() {
      this.queryOrder.query = ''
      this.getOrders()
    },
    editOrder(order) {
      // console.log(order)
      this.dialogOrderVisible = true
    },
    async flowOrder() {
      const { data: res } = await this.$axios.get('kuaidi/' + '1106975712662')
      if (res.meta.status !== 200) return this.$message.error('获取数据失败')
      this.orderAddress.address = res.data
      this.dialogFlowVisible = true
    }
  },
  filters: {
    dateformat(val) {
      return moment().format('YYYY-MM-DD hh:mm:ss')
    }
  }
}
</script>

<style lang='less' scoped>
</style>
